Ext.define('App.management.pm.view.PmSourceTableFrame', {
    extend: 'App.management.pm.view.PmBaseFrame',
    alias: ['widget.PmSourceTableFrame'],
    requires: ['App.datasource.view.SourceTableList'],
    listeners: {
        afterrender: function () {
            var me = this;
            me.initSourceTable();
        }
    },
    initSourceTable: function () {
        var me = this;
        if (Ext.isEmpty(app.project)) {
            return;
        }
        me.getTabPanel().mask('初始化数据...');
        Ext.Ajax.request({
            url: 'data_table/query_referenced_data_tables_by_project',
            params: {projectId: app.project.id},
            success: function (resp) {
                var dataTables = resp.result.data;
                for (var tableName in dataTables) {
                    var columns = [];
                    var dataTable = dataTables[tableName];
                    if (dataTable.type == 'iteration') {
                        columns.push({header: '迭代', dataIndex: 'iterationName'})
                    }
                    var tableFields = dataTable.tableFields;
                    var countWidth = 0;
                    Ext.Array.each(tableFields, function (tableField) {
                        countWidth += tableField.columnWidth;
                        columns.push({
                            header: tableField.fieldName,
                            dataIndex: tableField.fieldName
                        })
                    })
                    var report = Ext.create('App.datasource.view.SourceTableList', {
                        title: tableName,
                        columns: columns,
                        projectId: app.project.id,
                        dataTable: dataTable,
                        forceFit: countWidth > me.getWidth() ? false : true
                    })
                    me.addTabitem(report)
                }
                me.setActiveTab(0)
                me.getTabPanel().unmask();
            },
            failure: function () {
                me.getTabPanel().unmask();
            }
        })
    },
    tabitems: []
})